package org.dream.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.dream.domain.Stock;
import org.dream.service.StockService;
import org.dream.service.StockServiceImpl;
import org.dream.ui.chart.impl.KChart;

/**
 * 股票列表面板
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class StockTablePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTable stockTable;

	private StockService stockService = new StockServiceImpl();
	private int kind;
	private static final int ROWHEIGHT=21;

	public static JTabbedPane getJTabbedPane() {
		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.addTab("A股", null, new StockTablePanel(StockService.KIND_A));
		tabbedPane.addTab("自选股", null, new StockTablePanel(StockService.KIND_MYSELECT));
		tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
		tabbedPane.setSelectedIndex(1);
		return tabbedPane;
	}

	private StockTablePanel(int kind) {
		super(new BorderLayout());
		this.kind=kind;
		addColumnNames();// 添加表格列名称
		// addStockKindBox(); // 添加股票分类Box
		addStockDataTable();// 初始化股票数据表格\

	}

	/**
	 * 添加表格列名称
	 */
	private Vector<String> addColumnNames() {
		Vector<String> columnNames;
		columnNames = new Vector<String>();
		columnNames.add("序号");
		columnNames.add("股票代码");
		columnNames.add("股票名称");
		columnNames.add("涨幅");
		columnNames.add("现价");
		columnNames.add("涨跌");
		columnNames.add("查看详细");
		columnNames.add("查看K线");
		columnNames.add("查看折线");
		columnNames.add("量线");
		columnNames.add("量表");
		return columnNames;
	}

	/**
	 * 得到股票表格数据
	 */
	public Vector<Vector<Object>> getStockTableRowData() {
		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		List<Stock> stockList = stockService.findStocks(this.kind);

		for (int i = 0; i < stockList.size(); i++) {
			Vector<Object> row = new Vector<Object>();
			row.add("" + (i + 1));
			row.add(stockList.get(i).getCode());
			row.add(stockList.get(i).getName());
			row.add(stockList.get(i).getJlScore());
			row.add(stockList.get(i).getStime());
			row.add(stockList.get(i).getEtime());
			row.add("查看日线数据");
			row.add("查看K线");
			row.add("查看折线");
			row.add("量线");
			row.add("量表");
			data.add(row);
		}
		return data;
	}

	/**
	 * 初始化股票数据表格
	 */
	private void addStockDataTable() {

		stockTable = new JTable(getStockTableRowData(), addColumnNames());
		stockTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (stockTable.getSelectedRow() == -1) {
					return;
				}
				// 股票代码
				String stockCode = (String) stockTable.getModel().getValueAt(stockTable.getSelectedRow(), 1);
				Stock stock = stockService.getStockByCode(stockCode);
				switch (stockTable.getSelectedColumn()) {
					case 6:// 查看日线
						MainFrame.getInstanse().setCenterPanel(new DaydealTablePanel(stock));
						break;
					case 2:// 查看K线
						MainFrame.getInstanse().setCenterPanel(new KChart(stockCode, null, null));
						break;
					case 8:// 查看折线
							// UIHelper.setTopMainPanel(StockTablePanel.this,
							// new
							// StockDAO().getStockByCode(stockCode).createPolylinePanel());
						break;
					case 9:// 查看量线
							// MainFrame.getInstanse().setCenterPanel(
							// new VolumnChart(stockCode));
						break;
					case 10:// 查看量表
						MainFrame.getInstanse().setCenterPanel(new VolumeTablePanel(stock));
						break;
				}

			}
		});
		stockTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
		stockTable.setRowHeight(ROWHEIGHT);
		add(new JScrollPane(stockTable), BorderLayout.CENTER);
	}

	/**
	 * 更新数据表格数据
	 */
	private void refreshStockDataTable() {
		DefaultTableModel tableModel = new DefaultTableModel(getStockTableRowData(), addColumnNames());
		stockTable.setModel(tableModel);
		stockTable.updateUI();
	}
}
